#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &x : v) cin >> x;
  vector<vector<int>> dp(60, vector<int>(n, -1));
  for (int i = 0; i < n; i ++) dp[v[i]][i] = i + 1;
  int ans = 0;
  debug(dp);
  for (int k = 2; k < 60; k ++) {
    for (int i = 0; i < n; i ++) {
      if (dp[k - 1][i] == -1) continue;
      if (dp[k - 1][i] == n) continue;
      dp[k][i] = dp[k - 1][dp[k - 1][i]];
      if (dp[k][i] != -1) ans = max(ans, k);
    }
  }
  cout << ans << endl;
}